package com.wcpdoc.api.controller;

import com.wcpdoc.base.entity.TagInfo;
import com.wcpdoc.base.entity.UserTagInfo;
import com.wcpdoc.base.entity.UserTagInfoFrom;
import com.wcpdoc.base.service.TagInfoService;
import com.wcpdoc.base.service.TagUserService;
import com.wcpdoc.core.controller.BaseController;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户标签
 */
@Controller
@RequestMapping("/api/tag")
public class TagUserController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(TagUserController.class);

	@Resource
	private TagUserService tagUserService;

	@Resource
	private TagInfoService tagInfoService;

	/**
	 * 新增标签列表
	 */
	@RequestMapping("/addTagUser")
	@ResponseBody
	public PageResult addTagUser(UserTagInfoFrom userTagInfoFrom) {
		try {
			log.info(userTagInfoFrom.toString());
			String userId = userTagInfoFrom.getUserId();

			// 检查 tag 表里是否存在
			TagInfo tagInfo = tagInfoService.queryBYLabel(userTagInfoFrom.getTagValue());
			String tagId ="";
			if (tagInfo ==null){
				tagId = UUID.randomUUID().toString();
				tagInfoService.addTag(new TagInfo(userTagInfoFrom.getTagValue(), tagId));
			}else {
				tagId = tagInfo.getValue();
			}
			// 检查用户是否保存过标签
			UserTagInfo userTagInfo = tagUserService.queryTagByTag(userId,tagId);
			if (userTagInfo ==null){
				tagUserService.addTagUser(new UserTagInfo(userId,tagId));
			}

			return PageResultEx.ok().data(userId);
		} catch (Exception e) {
			log.error("用户列表错误：", e);
			return PageResult.err();
		}
	}



}
